<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE html>
<html>
  <head>
    <title>教材吧-搜索结果</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<%@ include file="/base/jsp/includeweb.jsp"%>
    <link type="text/css" rel="stylesheet" href="${path}/base/css/book.css"/>
    <style>
    .page-wrapper{padding-left:18%;}
    </style>
  </head>
  <body>
  <%@ include file="/base/jsp/header.jsp"%>
  <%@ include file="/base/jsp/search-header.jsp"%>
  <div id="content-wrapper" class="content-wrapper border-top-org clear">
  </div>
  <%@ include file="/base/jsp/page.jsp"%>
  <%@ include file="/base/jsp/footer.jsp"%>
  </body>
  <script type="text/javascript">
  var pageIndex = 0;
  var pageSize = 5;
  var pageT = 0;
  var userid = "${sessionScope.userid}";
  var key = getParameter("key");
  if(isDefine(key)){
      $("#keyword").val(key);
  }else{
     location.href="webindex.do";
  }
  
  function getBooks(key,pageSize,append){
       $(".search-book-wrapper").remove();
       request("listBook.do","name="+key+"&pageIndex="+pageIndex+"&pageSize="+pageSize,function(data){
		var d = data.data;
		if(d.length < 1){
			alert("此书暂未收藏，请与管理员联系");
		}
		for(var i in d){
		    var row = $('<div class="search-book-wrapper clear"><a target="_blank" class="book-img"><img/></a>'
		    +'<div class="search-book-info clear"><div class="name"><a target="_blank"></a></div><div class="price"></div>'
		    +'<div class="author-wrapper"><span class="author"></span>著/<span class="publisher"></span></div>'
		    +'<div class="stars clear"></div><div class="desc"></div><div class="reportory hide"></div>'
		    +'<div class="button"><input type="button" class="but-org cart-but" value="加入购物车">'
		    +'<input type="button" class="but-org buy-btn" value="立即购买"></div></div></div>');
		    var book = d[i];
		   row.attr("id",book.id);
		   row.find(".book-img").attr("href","bookdetail.do?id="+book.id);
		   row.find(".book-img img").attr("src",getImg(book.img));
		   row.find(".name a").attr("href","bookdetail.do?id="+book.id);
		   row.find(".name a").text(book.name);
		   row.find(".price").text("￥"+book.priceSelling);
		   row.find(".author").text(book.author);
		   row.find(".publisher").text(book.publisher);
		   row.find(".reportory").text(book.reportory);
		   for(var i = 0 ; i < book.rate ; i++){
		      var img = $('<img src="${path}/base/img/star.png"/>');
		      row.find(".stars").append(img);
		   }
		   row.find(".desc").text(book.description);
		   $(".content-wrapper").append(row);
		}
		$(".buy-btn").click(function(){
            if(!isDefine(userid)){
                alert("请先登录");
                location.href = "weblogin.do";
                return;
            }
            var bid = $(this).parents(".search-book-wrapper").attr("id");
            location.href="webaddorder.do?userid="+userid+"&bookid="+bid+"&count=1&type=1";
        })
        
        $(".cart-but").click(function(){
          if(!isDefine(userid)){
                alert("请先登录");
                location.href = "weblogin.do";
                return;
           }
          if(confirm("确定要加入购物车么？")){
              var bookid = $(this).parents(".search-book-wrapper").attr("id");
              request("saveCart.do","userid="+userid+"&bookid="+bookid+"&count=1",function(data){
                 if(data.success==true){
                    alert("添加成功");
                 }else{
                    alert("添加失败");
                 }
              });
          }
        })
        var total = data.total;
		//计算总页数
		pageT = Math.ceil(total / pageSize);
		//对分页进行处理
		if(pageT > 1){
		    $(".page-wrapper").show();
		    $(".page-num").remove();
		    for(var i = 1 ; i <= pageT ; i++){
		        var row = $('<a class="page-num" href="#"></a>');
		        row.text(i);
		        row.attr("name","page-"+(i-1));
		        row.insertBefore($(".next-page"));
		        if(pageIndex == (i-1)){
		           row.addClass("now");
		        }
		    }
		    $(".page-num").click(function(){
		        if(!$(this).hasClass("now")){
		            pageIndex = parseInt($(this).text())-1;
		            getBooks(key,pageSize,true);
		         }
		    });
		}else{
		    $(".page-wrapper").hide();
		}
	});
  }

  $(function(){
     getBooks(key,pageSize,false);
     $(".pre-page").click(function(){
		        if(pageIndex == 0){
		            alert("当前已经是第一页");
		            return;
		        }
		        pageIndex--;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        });
		        getBooks(key,pageSize,false);
		    });
		    
		$(".next-page").click(function(){
		        if(pageIndex == pageT-1){
		            alert("当前已经是最后一页");
		            return;
		        }
		        pageIndex++;
		        $(".page-wrapper a").each(function(){
		            $(this).removeClass("now");
		            if($(this).attr("name") == "page-"+pageIndex){
		                $(this).addClass("now");
		            }
		        })
		        getBooks(key,pageSize,false);
		   });

  })
  
  
  
  </script>
</html>